package com.yanfan.energy.service.impl;

import com.yanfan.energy.domain.*;
import com.yanfan.energy.domain.enums.ProductionStaffEnums;
import com.yanfan.energy.mapper.EvaluationDao;
import com.yanfan.energy.mapper.ProductionStaffDao;
import com.yanfan.energy.mapper.QualityTestingDao;
import com.yanfan.energy.service.ProductionStaffService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (ProductionStaff)表服务实现类
 *
 * @author makejava
 * @since 2023-01-30 14:21:49
 */
@Service("productionStaffService")
public class ProductionStaffServiceImpl implements ProductionStaffService {
    @Resource
    QualityTestingDao qualityTestingDao;
    @Resource
    EvaluationDao evaluationDao;
    @Resource
    private ProductionStaffDao productionStaffDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ProductionStaff queryById(Long id) {
        return this.productionStaffDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param productionStaff 筛选条件
     * @return 查询结果
     */
    @Override
    public List<ProductionStaff> queryByPage(ProductionStaffRequest productionStaff) {
        List<ProductionStaff> productionStaffs = this.productionStaffDao.queryAllByLimit(productionStaff);
        if (CollectionUtils.isEmpty(productionStaffs)) {
            return productionStaffs;
        }
        for (ProductionStaff staff : productionStaffs) {
            if (staff.getRate() != null) {
                staff.setRateStr(staff.getRate() + "%");
            }
            staff.setSexName(ProductionStaffEnums.SexEnums.getEnumByType(staff.getSex()));
            staff.setProductSateName(ProductionStaffEnums.ProductionStateEnums.getEnumByType(staff.getProductSate()));
        }
        return productionStaffs;
    }

    /**
     * 新增数据
     *
     * @param productionStaff 实例对象
     * @return 实例对象
     */
    @Override
    public ProductionStaff insert(ProductionStaff productionStaff) {
        this.productionStaffDao.insert(productionStaff);
        return productionStaff;
    }

    /**
     * 修改数据
     *
     * @param productionStaff 实例对象
     * @return 实例对象
     */
    @Override
    public ProductionStaff update(ProductionStaff productionStaff) {
        this.productionStaffDao.update(productionStaff);
        return this.queryById(productionStaff.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.productionStaffDao.deleteById(id) > 0;
    }

    @Override
    public Map<String, Object> detailInfo(Long userId) {
        Map<String, Object> resMap = new HashMap<>();
        /**
         * 前5大不良
         */
        QualityTesting qualityTesting = new QualityTesting();
        qualityTesting.setOperatorId(userId);
        List<QualityTesting> qualityTestings = qualityTestingDao.queryAll(qualityTesting);
        Map qualityMap = new HashMap();
        if (!CollectionUtils.isEmpty(qualityTestings)) {
            int flagIndex = qualityTestings.size() > 5 ? 5 : qualityTestings.size();
            for (int i = 0; i < flagIndex; i++) {
                QualityTesting quality = qualityTestings.get(i);
                qualityMap.put(quality.getProductName(), quality.getFailAmount());
            }
        }
        resMap.put("unqualified", qualityMap);
        /**
         *  人员时段生产  TimeProduction
         *  // TODO 任务这块还没有做 所有暂时数据写死
         */
        Map timeProductionMap = new HashMap();
        timeProductionMap.put("7", "21");
        timeProductionMap.put("8", "11");
        timeProductionMap.put("9", "10");
        timeProductionMap.put("10", "1");
        timeProductionMap.put("11", "15");
        timeProductionMap.put("12", "21");
        timeProductionMap.put("13", "31");
        timeProductionMap.put("14", "19");
        timeProductionMap.put("15", "18");
        timeProductionMap.put("15", "17");
        timeProductionMap.put("16", "18");
        timeProductionMap.put("17", "25");
        timeProductionMap.put("18", "18");
        resMap.put("timeProduction", timeProductionMap);

        /**
         * 绩效打分
         */
        Map evaluationMap = new HashMap();
        EvaluationRequest evaluationRequest = new EvaluationRequest();
        evaluationRequest.setUserId(userId);
        Calendar calendar = Calendar.getInstance();
        int month = calendar.get(Calendar.MONTH) + 1;
        int year = calendar.get(Calendar.YEAR);
        evaluationRequest.setAssessYear(String.valueOf(year));
        List<Evaluation> evaluations = evaluationDao.queryAllByLimit(evaluationRequest);
        int begin = 1;
        if (month > 5) {
            begin = month - 4;
        }
        for (int i = begin; i <= month; i++) {
            evaluationMap.put(i + "月", 0.0);
        }
        if (!CollectionUtils.isEmpty(evaluations)) {
            for (Evaluation evaluation : evaluations) {
                if (evaluationMap.containsKey(evaluation.getAssessMonth())) {
                    evaluationMap.put(evaluation.getAssessMonth(), evaluation.getGrade());
                }
            }
        }
        resMap.put("evaluation", evaluationMap);
        return resMap;
    }
}
